package weaver.interfaces.jjy.duss.util;

import weaver.file.ImageFileManager;
import java.io.*;

/**
 * @program: ecology-9-demo
 * @description: CreateFileUtilForImage
 * @author: Dusongsong
 * @create: 2019-12-20
 */
public class CreateFileUtilForImage {
    /**
     *
     * @param filepath 附件路径
     * @param createrid 创建人id
     * @param imagefilename 附件名称
     * @return
     */
    public int  doCreate(String filepath,String createrid,String imagefilename,boolean iszip){
        if("".equals(filepath) ||  "".equals(createrid)  || "".equals(imagefilename)){
            return -100;
        }

        if("".equals(createrid)){
            return -102;
        }
        if("".equals(imagefilename)){
            return -103;
        }

        int sender = Integer.parseInt(createrid);
        int imagefileid = 0;
        byte []content = null;
        try {
            // 创建输入流和内存输出流
            int byteread;
            byte data[] = new byte[1024];
            InputStream input = new FileInputStream(new File(filepath));
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            while ((byteread = input.read(data)) != -1) {
                out.write(data, 0, byteread);
                out.flush();
            }
            content = out.toByteArray();
            input.close();
            out.close();
            imagefileid = getImageFile(content,imagefilename);
        } catch (Exception e) {
            e.printStackTrace();
            return -104;
        }

        return imagefileid;
    }

    //生成附件
    private int getImageFile(byte []content, String name){
        ImageFileManager imageFileManager = new ImageFileManager();
        imageFileManager.resetParameter();
        imageFileManager.setData(content);
        imageFileManager.setImagFileName(name);
        imageFileManager.setComefrom(" from accessary");
        int imagefileid = imageFileManager.saveImageFile();
        return imagefileid;
    }
}
